package net.towee.model.discussion.impl;

import java.util.LinkedList;
import java.util.List;

import net.towee.model.discussion.Discussion;
import net.towee.model.discussion.DiscussionSubject;
import net.towee.model.discussion.Message;
import net.towee.server.persistence.impl.MongoDbStore;

import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.PostLoad;
import com.google.code.morphia.annotations.Reference;

/**
 * Discussion implementation
 * @author c58
 */
@Embedded
public class DiscussionImpl implements Discussion {
	private LinkedList<Message> messages;
	private int count;

	public DiscussionImpl() {
		this.messages = new LinkedList<Message>();
		this.count = 0;
	}
	
	@PostLoad
	public void createEmptyFields() {
		if(messages == null)
			messages = new LinkedList<Message>();
	}

	@Override
	public void addMessage(Message comment) {
		count++;
		messages.addLast(comment);
	}

	@Override
	public void removeMessage(Message comment) {
		messages.remove(comment);
	}

	@Override
	public LinkedList<Message> getMessagesList() {
		return messages;
	}

	@Override
	public int getCount() {
		return count;
	}
	@Override
	public void setMessages(LinkedList<Message> subList) {
		this.messages = subList;
	}
	@Override
	public void addMessages(boolean direction, List<Message> comment) {
		if(direction)
			messages.addAll(comment);
		else
			for(int i=comment.size()-1;i>=0;i--)
				messages.addFirst( comment.get(i) );
	}
	@Override
	public Message getMessage(String id) {
		int index = messages.indexOf( new MessageImpl(id) );
		
		if(index >= 0)
			return messages.get(index);
		
		return null;
	}
}
